package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import java.util.List;

import dianmobile.eistarandroid.beans.AccountEntity;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.utils.DataUtils;

/**
 * 账号列表的adapter
 * Created by dianmobile_yy on 15/11/24.
 */
public class AccountListAdapter  extends BaseAdapter{

    private List<AccountEntity> list;
    private LayoutInflater inflater;

    public AccountListAdapter(Context context, List<AccountEntity> list) {
        this.list = list;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.listitem_account, null);
        CircleImageView accountHead = (CircleImageView)convertView.findViewById(R.id.iv_account_head);
        TextView account = (TextView)convertView.findViewById(R.id.tv_account);
        ImageButton delete = (ImageButton)convertView.findViewById(R.id.btn_delete);

        final AccountEntity item = list.get(position);
        String url = DataUtils.getUserHeadUrlFromName(item.getAccount());
        RequestFactory.makeNetworkImage(accountHead, url, R.drawable.default_face, 0);
        account.setText(item.getAccount());
        if(item.isLogin()) {
            delete.setBackgroundColor(Color.GREEN);
        } else {
            delete.setVisibility(View.INVISIBLE);
        }

        if (item.isEdit()) {
            delete.setVisibility(View.VISIBLE);
            delete.setClickable(true);
            delete.setBackgroundColor(Color.RED);
        } else {
            delete.setClickable(false);
        }

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isEdit()) {
                    list.remove(position);
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }
}
